package br.com.center.loja.mb.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.center.loja.data.RepositoryFactoryJPA;
import br.com.center.loja.model.Neighborhood;


@FacesConverter(forClass=Neighborhood.class, value="neighborhoodConverter")
public class NeighborhoodConverter implements Converter
{

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) 
	{
		try {
			return RepositoryFactoryJPA
					.create(Neighborhood.class)
					.select(Long.valueOf(value));
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) 
	{
		try {
			return ((Neighborhood)value).getCode().toString();			
		} catch (Exception e) {
			return null;
		}
	}

}
